<?php
//首页显示
namespace Home\Controller;
use Think\Controller;
class StoreController extends CommonController {
	public function _initialize() {
		parent::_initialize();
		$id=I('get.id');
		if($id){
			//获取模版基本信息
	    	$store=M('store');
	    	$store_info=$store->find($id);
	    	$this->assign('store_info',$store_info);
			//dump($store_info);die;
	    	if($store_info['status']==0){
	    		$this->error('该店铺已关闭');
	    	}
			//店铺详情
			$store_template=M('store_template');
	    	$store_template_info=$store_template->where('store_id='.$id)->find();
	    	$this->assign('store_template_info',$store_template_info);
	    	$banners=explode(',',$store_template_info['banners']);
	    	$this->assign('banners',$banners);
	    	//店铺所在地
	    	$address['Province']=getProvinceInfo($store_info['province']);
	    	$address['City']=getCityInfo($store_info['city']);
	    	$address['District']=getDistrictInfo($store_info['district']);
	    	$this->assign('address',$address);
	    	//店铺经营时间
	    	$cha_time=round((time()-$store_info['addtime'])/ 86400);
	    	$year=floor($cha_time/365);
	    	$month_day=$cha_time-$year*365;
	    	$month=floor($month_day/30);
	    	$day=$month_day-30*$month;
	    	$cha_time=$year.'年'.$month.'月'.$day.'日';
	    	$this->assign('cha_time',$cha_time);
	    	
	    	//判断收藏
	    	$users_storecollect=M('users_storecollect');
	    	if(session('user_id')){
	    		
				$newstoreinfo['shore_id']=$id;
				$newstoreinfo['user_id']=session('user_id');
				$rsstoreinfo=$users_storecollect->where($newstoreinfo)->select();
				if($rsstoreinfo){
					$rsstoreinfo_status=1;
				}else{
					$rsstoreinfo_status=0;
				}
	   		}else{
		   		$rsstoreinfo_status=0;
	   		}
	    	$this->assign('rsstoreinfo_status',$rsstoreinfo_status);
	    }
	}
    public function index(){
    	$id=I('get.id');
    	
    	//促销商品
    	$store_class=M('store_class');
    	$shop=M('shop');
    	$store_class_list=$store_class->where('store_id='.$id)->select();
    	foreach($store_class_list as $key=>$value){
			$shop_ids=explode(',',$value['shop_ids']);
			$store_class_list[$key]['shop_list']=array();
			foreach($shop_ids as $k=>$v){
				$shop_info=$shop->field('id,image,title,price')->find($v);
				$store_class_list[$key]['shop_list'][$k]=$shop_info;
			}
		}
		$this->assign('store_class_list',$store_class_list);
    	
    	//获取模版
    	$store_template=M('store_template');
    	$store_template_info=$store_template->where('store_id='.$id)->find();
    	$template=$store_template_info['template'];
    	$template=$template?$template:'demo';
		$this->display('/Store/template/'.$template.'/index');
	}
	public function list_store(){
		//分类
		$store_classification=M('store_classification');
		$store_classifications=$store_classification->select();
    	$this->assign('store_classifications',$store_classifications);
    	//区域
    	$store=M('store');
    	$area_list=$store->field('province')->where('province is not null and province!=0 and  status=1')->group('province')->select();
    	$this->assign('area_list',$area_list);
    	//店铺
    	$where='yt_store.status=1 ';
    	if($_GET['type_id']){
    		$where.=' AND FIND_IN_SET("'.$_GET['type_id'].'",yt_store.store_classification_type)';
    	}
    	if($_GET['area_id']){
    		$where.=' AND yt_store.province='.$_GET['area_id'];
    	}
    	if($_GET['name']){
    		$where.=' AND yt_store.title like "%'.$_GET['name'].'%"';
    	}
    	
    	$count=$store
				->field('yt_store_template.portrait,yt_store_template.logo,yt_store.title,yt_store.id')
				->join('yt_store_template ON yt_store.id = yt_store_template.store_id','left')
				->where($where)
				->count();
		
    	$this->assign('count',$count);
		    $Page       = new \Think\Page($count,9);// 实例化分页类 传入总记录数
		    $count_page=ceil($count/9);
		    $this->assign('count_page',$count_page);
		    $Page->setConfig('header','条记录');
		    $Page->setConfig('prev','上一页');
		    $Page->setConfig('next','下一页');
		    $show       = $Page->show();// 分页显示输出			
    		$store_list=$store
    				->field('yt_store_template.portrait,yt_store_template.logo,yt_store.title,yt_store.id')
    				->join('yt_store_template ON yt_store.id = yt_store_template.store_id','left')
    				->where($where)
    				->limit($Page->firstRow.','.$Page->listRows)
    				->select();
    	$this->assign('page',$show);// 赋值分页输出
    	$this->assign('store_list',$store_list);
    	$this->display();
	}
	public function type(){
		$type_id=I('get.shop_id');
		$store_id=I('get.id');
		$shop=M('shop');
		$store_type=M('store_type');
		$type_info=$store_type->find($type_id);
		$this->assign('type_info',$type_info);
		//排序
   		$order=I('get.order',0);
   		$this->assign('order',$order);
   		$orders=array(
   			'0' => 'id desc', 
   			'1' => 'sale_num desc', 
   			'2' => 'price desc', 
   		);
		if($type_id==0){
			$count=$shop->where('ishidden=0 and store_id='.$store_id)->count();
			$this->assign('count',$count);
		    $Page       = new \Think\Page($count,4);// 实例化分页类 传入总记录数
		    $count_page=ceil($count/4);
		    $this->assign('count_page',$count_page);
		    $Page->setConfig('header','条记录');
		    $Page->setConfig('prev','上一页');
		    $Page->setConfig('next','下一页');
		    $show       = $Page->show();// 分页显示输出
			$shop_list=$shop->where('ishidden=0 and store_id='.$store_id)->order($orders[$order])->limit($Page->firstRow.','.$Page->listRows)->select();
		}else{
			$store_type=M('store_type');
			$types=$this->get_types($type_id);
			//$where['store_type_id']=array('in',$types);
            $where['_string'] = 'FIND_IN_SET('.$type_id.',store_type_id)';
			$where['ishidden']=0;
			$count=$shop->where($where)->count();
			$this->assign('count',$count);
		    $Page       = new \Think\Page($count,4);// 实例化分页类 传入总记录数
		    $count_page=ceil($count/4);
		    $this->assign('count_page',$count_page);
		    $Page->setConfig('header','条记录');
		    $Page->setConfig('prev','上一页');
		    $Page->setConfig('next','下一页');
		    $show       = $Page->show();// 分页显示输出
			$shop_list=$shop->where($where)->order($orders[$order])->limit($Page->firstRow.','.$Page->listRows)->select();
		}
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('shop_list',$shop_list);
		$this->display();
	}
	
	public function get_types($id){
		$store_type=M('store_type');
		$types=$store_type->where('parentid='.$id)->select();
		if($types){
			foreach($types as $key=>$value){
				$son_types=$store_type->where('parentid='.$value['id'])->select();
				if($son_types){
					$ids=$this->get_types($value['id']);
					$id=$id.','.$ids;
				}else{
					$id=$id.','.$value['id'];
				}
				
				
			}
		}
		return $id;
		
	}

}
?>
